#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc_core.h"



u8 RM010_Gamma_r[50]={0x00,0x01,   //r0
	                    0x02,0x03,   //r7
	                    0x04,0x05,   //r11
	                    0x06,0x07,   //r15
	                    0x08,0x09,   //r23
	                    0x0A,0x0B,   //r31
	                    0x0C,0x0D,   //r39
	                    0x0E,0x0F,   //r47
                      0x10,0x11,   //r63
                      0x12,0x13,   //r79
                      0x14,0x15,   //r95
                      0x16,0x17,   //r111
                      0x18,0x19,   //r127
                      0x1A,0x1B,   //r143
                      0x1C,0x1D,   //r159
                      0x1E,0x1F,   //r175
                      0x20,0x21,   //r191
	                    0x22,0x23,   //r207
	                    0x24,0x25,   //r215
	                    0x26,0x27,   //r223
	                    0x28,0x29,   //r231
	                    0x2A,0x2B,   //r239
											0x2D,0x2F,   //r243
											0x30,0x31,   //r247
                      0x32,0x33};  //r255

u8 RM010_Gamma_g[50]={0x34,0x35,   //g0
                      0x36,0x37,   //g7
                      0x38,0x39,   //g11
                      0x3A,0x3B,   //g15
	                    0x3D,0x3F,   //g23
                      0x40,0x41,   //g31
	                    0x42,0x43,   //g39
	                    0x44,0x45,   //g47
                      0x46,0x47,   //g63
	                    0x48,0x49,   //g79
                      0x4A,0x4B,   //g95
	                    0x4C,0x4D,   //g111
                      0x4E,0x4F,   //g127
	                    0x50,0x51,   //g143
                      0x52,0x53,   //g159
	                    0x54,0x55,   //g175
                      0x56,0x58,   //g191
	                    0x59,0x5A,   //g207
	                    0x5B,0x5C,   //g215
	                    0x5D,0x5E,   //g223
	                    0x5F,0x60,   //g231
	                    0x61,0x62,   //g239
											0x63,0x64,   //g243
											0x65,0x66,   //g247
                      0x67,0x68};  //g255

u8 RM010_Gamma_b[50]={0x69,0x6A,   //b0
                      0x6B,0x6C,   //b7
                      0x6D,0x6E,   //b11
                      0x6F,0x70,   //b15
	                    0x71,0x72,   //b23
                      0x73,0x74,   //b31
	                    0x75,0x76,   //b39
	                    0x77,0x78,   //b47
                      0x79,0x7A,   //b63
	                    0x7B,0x7C,   //b79
                      0x7D,0x7E,   //b95
	                    0x7F,0x80,   //b111
                      0x81,0x82,   //b127
	                    0x83,0x84,   //b143
                      0x85,0x86,   //b159
	                    0x87,0x88,   //b175
                      0x89,0x8A,   //b191
	                    0x8B,0x8C,   //b207
	                    0x8D,0x8E,   //b215
	                    0x8F,0x90,   //b223
	                    0x91,0x92,   //b231
	                    0x93,0x94,   //b239
											0x95,0x96,   //b243
											0x97,0x98,   //b247
                      0x99,0x9A};  //b255

/********************************************************************************************
********************************************************************************************
    RM6D010  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    RM6D010  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    RM6D010  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    RM6D010  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    RM6D010  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
********************************************************************************************
********************************************************************************************/
void RM6D01X_Write_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	if((SigMode == SPI3_Mode)||(SigMode == SPI4_Mode))          //3线 4线8bit SPI 模式
    {
        SPI_Write_code(SigMode,channel,buffer[3],buffer[4]);  
    }
    else if((SigMode == Mipi_Mode)||(SigMode == CMD_Mode))
    {
        if(SigMode != CMD_Mode)
          SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
				else
				  SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
        delay_ms(2);
        SSD2828_W_Array(SigMode,channel,buffer,2);
        delay_ms(5);
        SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    }
    buffer[4] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写寄存器状态  ：ok  
}

void RM6D01X_Read_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    if(SigMode == SPI3_Mode)          //3线8bit SPI 模式
    {

    }
    else if(SigMode == SPI4_Mode)          //4线8bit SPI 模式
    {
    
    }
    else if((SigMode == Mipi_Mode)||(SigMode == CMD_Mode))
    {
        SSD2828_W_Reg(SigMode,channel,0xC1,0x0002); 
			  delay_ms(5);
			  SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x00D0);
        delay_ms(5);
        SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);
        SSD2828_W_Reg(SigMode,channel,0xBD,0x0000);                                    
        for(i=0;i<3;i++)
        {
            SSD2828_W_Cmd(SigMode,channel,0xBF);
            SSD2828_W_Data(SigMode,channel,buffer[3]);
            if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI   
                SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);  
            delay_ms(5);
            buffer[4]=SSD2828_R_Reg(SigMode,channel,0xFF);

        }                              
        SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
        delay_ms(15);
        STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 寄存器数据  ：ok  	
    }
}

void RM6D01X_Write_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[3];
    u8 gamma_write_num=50;
    
    if((SigMode == SPI3_Mode)||(SigMode == SPI4_Mode))          //3线 4线8bit SPI 模式
    {   			
        if((buffer[3]&0x03) == 0x00)
            SPI_Write_code(SigMode,channel,0xFE,0x02);
        else if((buffer[3]&0x03) == 0x01)
            SPI_Write_code(SigMode,channel,0xFE,0x03);
            buffer1[0] = 0x02;
            buffer1[1] = 0xEF;
            buffer1[2] = 0x01;   
		        SSD2828_W_Array(SigMode,channel,buffer1,0);
		        delay_ms(15);		
        for( i = 0;i<gamma_write_num;i++)
        {	                                                                   
            SPI_Write_code(SigMode,channel,((buffer[3]==0x00) ? ((buffer[0]==RM6D010) ? RM010_Gamma_r[i] : RM010_Gamma_r[i]) : \
                                           ((buffer[3]==0x34) ? ((buffer[0]==RM6D010) ? RM010_Gamma_g[i] : RM010_Gamma_g[i]) : \
                                                                ((buffer[0]==RM6D010) ? RM010_Gamma_b[i] : RM010_Gamma_b[i]))),buffer[i+4]);     
            delay_ms(1);
        }                                    
    }
    else if((SigMode == Mipi_Mode)||(SigMode == CMD_Mode))
    {
			   if(SigMode != CMD_Mode)
            SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
				 else
			      SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
         delay_ms(20);
////				buffer1[0] = 0x02;
////        buffer1[1] = 0xFE;
////        buffer1[2] = 0x01;   
////		    SSD2828_W_Array(SigMode,channel,buffer1,0);
////				 delay_ms(2);
////				buffer1[0] = 0x02;
////        buffer1[1] = 0xA7;
////        buffer1[2] = 0x00;   
////		    SSD2828_W_Array(SigMode,channel,buffer1,0);
////				 delay_ms(2);
//        buffer1[0] = 0x02;
//        buffer1[1] = 0xFE;
//			  buffer1[2] = 0x02;
//        SSD2828_W_Array(SigMode,channel,buffer1,0); 
//				delay_ms(2); 
        buffer1[0] = 0x02;
        buffer1[1] = 0xEF;
        buffer1[2] = 0x01;   
		    SSD2828_W_Array(SigMode,channel,buffer1,0);
		    delay_ms(10);			
        for( i = 0;i<gamma_write_num;i++)
        {	                                                                   			
					if((buffer[3]&0xf0)==0x00)
							SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM010_Gamma_r[i]);  
					else if((buffer[3]&0xf0)==0x40)
						  SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM010_Gamma_g[i]); 
					else if((buffer[3]&0xf0)==0x80)
							SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM010_Gamma_b[i]); 																		
					delay_ms(5);
        }	
		buffer1[0] = 0x02;
    buffer1[1] = 0xF0;
    buffer1[2] = 0x01;    
	  SSD2828_W_Array(SigMode,channel,buffer1,0);
    delay_ms(2);
		buffer1[0] = 0x02;
    buffer1[1] = 0xEF;
    buffer1[2] = 0x00;    
		SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(2);		
		buffer1[0] = 0x02;
    buffer1[1] = 0xF0;
    buffer1[2] = 0x00;    
	  SSD2828_W_Array(SigMode,channel,buffer1,0);		
    delay_ms(2);
        SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);                              
    }
    buffer[4] = Uart_Error_None;
//    buffer[1] =  0x08;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok 
}

void RM6D01X_auto_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	 u8 buffer1[3];
    if((SigMode == SPI3_Mode)||(SigMode == SPI4_Mode))          //3线 4线8bit SPI 模式
    {                                                                  
        for( i = 0;i<buffer[2];i++)
        {	                                                                   
            SPI_Write_code(SigMode,channel,buffer[i+3],buffer[i+4]);     
            i++;
        }                                    
    }
    else if((SigMode == Mipi_Mode)||(SigMode == CMD_Mode))
    {
        if(SigMode != CMD_Mode)
            SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);  
				
//		buffer1[0] = 0x02;
//		buffer1[1] = 0xFE;
//		buffer1[2] = 0x02;              // GAMMA1  page1
//		SSD2828_W_Array(SigMode,channel,buffer1,0);	
//		SSD2828_W_Reg(SigMode,channel,0xBC,0x02);
				buffer1[0] = 0x02;
        buffer1[1] = 0xEF;
        buffer1[2] = 0x01;   
		    SSD2828_W_Array(SigMode,channel,buffer1,0);
		    delay_ms(10);			
        for( i = 0;i<buffer[2];i++)
        {	                                                                   
            SSD2828_W_Reg(SigMode,channel,0xBF,(buffer[i+4]<<8)+buffer[i+3]);      //RM6D010
            i++;
					 //delay_ms(10);
        }	
		buffer1[0] = 0x02;
    buffer1[1] = 0xF0;
    buffer1[2] = 0x01;    
	  SSD2828_W_Array(SigMode,channel,buffer1,0);
    delay_ms(2);
		buffer1[0] = 0x02;
    buffer1[1] = 0xEF;
    buffer1[2] = 0x00;    
		SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(2);		
		buffer1[0] = 0x02;
    buffer1[1] = 0xF0;
    buffer1[2] = 0x00;    
	  SSD2828_W_Array(SigMode,channel,buffer1,0);		
    delay_ms(2);
        SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);                              
    }
}

void RM6D01X_Read_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
//	  u8 buffer1[3];
	  u8 gamma_read_num=50;

			if((SigMode == Mipi_Mode)||(SigMode == CMD_Mode))
    {
        if(SigMode != CMD_Mode)
        SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
        SSD2828_W_Reg(SigMode,channel,0xBC,0x0002);
        SSD2828_W_Reg(SigMode,channel,0xBD,0x0000);   

//					if(buffer[0]==RM6D010)
//        {
////            if(buffer[1] == 0x09)
////                SSD2828_W_Reg(SigMode,channel,0xBF,0x02FE);
////            else if(buffer[1] == 0x19)
////                SSD2828_W_Reg(SigMode,channel,0xBF,0x03FE); 
//					if(buffer[4] == 0x00)
//    {
////        buffer1[0] = 0x02;
////        buffer1[1] = 0xFE;
////        buffer1[2] = 0x00;
////        SSD2828_W_Array(SigMode,channel,buffer1,0);
////        buffer1[0] = 0x02;
////        buffer1[1] = 0x38;
////        buffer1[2] = 0x00;
////        SSD2828_W_Array(SigMode,channel,buffer1,0); 

////        buffer1[0] = 0x02;
////        buffer1[1] = 0x53;
////        buffer1[2] = 0x20;
////        SSD2828_W_Array(SigMode,channel,buffer1,0);          

//        buffer1[0] = 0x02;
//        buffer1[1] = 0xFE;
//        buffer1[2] = 0x02;      // GAMMA1  page1
//        SSD2828_W_Array(SigMode,channel,buffer1,0); 
//    }
//    else if(buffer[4] == 0x01)
//    {
////        buffer1[0] = 0x02;
////        buffer1[1] = 0xFE;
////        buffer1[2] = 0x00;
////        SSD2828_W_Array(SigMode,channel,buffer1,0);
////        buffer1[0] = 0x02;
////        buffer1[1] = 0x53;
////        buffer1[2] = 0x20;
////        SSD2828_W_Array(SigMode,channel,buffer1,0);  

////        buffer1[0] = 0x02;
////        buffer1[1] = 0x39;
////        buffer1[2] = 0x00;
////        SSD2828_W_Array(SigMode,channel,buffer1,0);   

//        buffer1[0] = 0x02;
//        buffer1[1] = 0xFE;       
//        buffer1[2] = 0x03;      // GAMMA2  page7
//        SSD2828_W_Array(SigMode,channel,buffer1,0); 
//    }
//    else if(buffer[4] == 0x02)
//    {
////        buffer1[0] = 0x02;
////        buffer1[1] = 0xFE;
////        buffer1[2] = 0x00;
////        SSD2828_W_Array(SigMode,channel,buffer1,0);	
////        buffer1[0] = 0x02;
////        buffer1[1] = 0x38;
////        buffer1[2] = 0x00;
////        SSD2828_W_Array(SigMode,channel,buffer1,0);  
////        buffer1[0] = 0x02;
////        buffer1[1] = 0x53;
////        buffer1[2] = 0x60;
////        SSD2828_W_Array(SigMode,channel,buffer1,0);         
//        buffer1[0] = 0x02;
//        buffer1[1] = 0xFE;        
//        buffer1[2] = 0x03;      // GAMMA3  page8
//        SSD2828_W_Array(SigMode,channel,buffer1,0); 
//    }
//		else
//		{
//				buffer[4] = Uart_Error_Oth;
//				STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok  				
//				//goto RM69300_r_end;
//		}		
//        }
        if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI
            SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080); 
              //SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);				
        else
        SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
        SSD2828_W_Reg(SigMode,channel,0xC1,0x0001); 
        SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
        SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);
                            
        for( i = 0;i<gamma_read_num;i++)							
        {   
                SSD2828_W_Cmd(SigMode,channel,0xBF);
                SSD2828_W_Data(SigMode,channel,(buffer[3]==0x00) ? ((buffer[0]==RM6D010) ? RM010_Gamma_r[i] : RM010_Gamma_r[i]) : \
                                               (buffer[3]==0x42) ? ((buffer[0]==RM6D010) ? RM010_Gamma_g[i] : RM010_Gamma_g[i]) : \
                                                                   ((buffer[0]==RM6D010) ? RM010_Gamma_b[i] : RM010_Gamma_b[i]));
                delay_ms(25);
                if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI
                    SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA); 
                buffer[i+4]=SSD2828_R_Reg(SigMode,channel,0xFF);
        }                   
        SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);	                              
    }
    buffer[1] = 0x0C; //(buffer[0]==RM67160) ? 0x09 : 0x0C;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 Gamma数据  ：ok 
}


void RM6D01X_Gamma_OTP_Start(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 tmp;
	  u8 buffer1[3];
    if((SigMode == SPI3_Mode)||(SigMode == SPI4_Mode))          //3线 4线8bit SPI 模式
    {
        delay_ms(120); 
        SPI_Write_code(SigMode,channel,0xfe,0x00);
        SPI_Write_u8_Cmd(SigMode,channel,0x28); 
        delay_ms(100);  //otp
        SPI_Write_code(SigMode,channel,0xfe,0x01);
        SPI_Write_code(SigMode,channel,0xf0,0x10);
        SPI_Write_code(SigMode,channel,0xf2,0x03);
        SPI_Write_code(SigMode,channel,0xf3,0xA5); 
        SPI_Write_code(SigMode,channel,0xf4,0x5a);
        SPI_Write_code(SigMode,channel,0xf5,0x3c);
        delay_ms(500);         // wait for >400 ms 
        
        
        buffer[4] = Uart_Error_None;
        STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 OTP状态  ：ok
    }
    else if((SigMode == Mipi_Mode)||(SigMode == CMD_Mode))
    {
        delay_ms(120); 
        if(SigMode != CMD_Mode)
            SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
        delay_ms(5);
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);                                                                      

        buffer1[0] = 0x01;
        buffer1[1] = 0x28;
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
        delay_ms(200);
//        buffer1[0] = 0x01;
//        buffer1[1] = 0x10;
//        SSD2828_W_Array(SigMode,channel,buffer1,0);     // 关显示  
//				
//        delay_ms(100);  //otp
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x01;
        SSD2828_W_Array(SigMode,channel,buffer1,0);  
        if(buffer[1]==0x0A)  //otp GammaSet1
    {
        buffer1[0] = 0x02;
        buffer1[1] = 0xF0;
        buffer1[2] = 0x20;        
     
        SSD2828_W_Array(SigMode,channel,buffer1,0);     //OTP_CMD2_P1_PGM_EN          
    }
    if(buffer[1]==0x0D)  //otp GammaSet2
    {
        buffer1[0] = 0x02;
   
   
            buffer1[1] = 0xF0;
            buffer1[2] = 0x10;        
     
        SSD2828_W_Array(SigMode,channel,buffer1,0);     //OTP_CMD2_P6_PGM_EN  
    }
    if(buffer[1]==0x0E) //otp GammaSet3
    {		
        buffer1[0] = 0x02;
 
            buffer1[1] = 0xF6;
            buffer1[2] = 0x80;
    
        
        SSD2828_W_Array(SigMode,channel,buffer1,0);     //OTP_CMD2_P8_PGM_EN  
    }                                       

        delay_ms(200); 
        buffer1[1] = 0xF2; 
				buffer1[2] = 0x03;
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
        
        buffer1[1] = 0xF3; 
				buffer1[2] = 0xA5;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        
        buffer1[1] = 0xF4;
				buffer1[2] = 0x5A;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        
        buffer1[1] = 0xF5; 
				buffer1[2] = 0x3C;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
        delay_ms(800);         // wait for >400 ms   
        if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI
            SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);      
        else
            SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
        SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);
        SSD2828_W_Reg(SigMode,channel,0xC0,0x0001);
        buffer1[0] = 0x01;
        buffer1[1] = 0xEF;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI   烧录OTP
            SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
        delay_ms(100);															   								
        tmp=SSD2828_R_Reg(SigMode,channel,0xFF); 
        delay_ms(5);
        SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
        if (tmp&0x02)       //出错
        {
            buffer[4] = Uart_Error_Oth;
        }
        else                //正确
            buffer[4] = Uart_Error_None; 
        STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 OTP状态  ：ok                                       
    }
}

void RM6D01X_Read_OTP_Time(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{     
    u8 tmp;
    if(SigMode == SPI3_Mode)          //3线8bit SPI 模式
    {
 
    }
    else if(SigMode == SPI4_Mode)          //4线8bit SPI 模式
    {
    
    }
    else if((SigMode == Mipi_Mode)||(SigMode == CMD_Mode))
    {
        if(SigMode != CMD_Mode)
            SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
        delay_ms(5);
        SSD2828_W_Reg(SigMode,channel,0xBC,0x0002);
        SSD2828_W_Reg(SigMode,channel,0xBD,0x0000);
        
        if(buffer[0]==RM6D010)
        {
            SSD2828_W_Reg(SigMode,channel,0xBF,0x02FE); 
        }
        
        if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI
            SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);      
        else
            SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);        
        delay_ms(5);
        SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);
        SSD2828_W_Cmd(SigMode,channel,0xBF);
        SSD2828_W_Data(SigMode,channel,0xEE);
        if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI  
            SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);							
        delay_ms(50);                                 
        tmp=SSD2828_R_Reg(SigMode,channel,0xFF); 
        
        if (tmp&0x08)
            buffer[4] = 1;
        else if (tmp&0x10)
            buffer[4] = 2;
        else
            buffer[4] = 0;
        delay_ms(5);
        SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
        buffer[3] = Uart_Error_None;
        STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的OTP数据  ：ok                                  
    }
    buffer[4] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 OTP状态  ：ok

}

void RM6D01x_Gamma_switch(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[5];
    static u8 RM6D010_state = 0;    // 0=normal   1=idle  2=HBM
    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
    delay_ms(10);
    if(buffer[3] == 0x01)
    {
//        buffer1[0] = 0x02;
//        buffer1[1] = 0xFE;
//        buffer1[2] = 0x00;
//        SSD2828_W_Array(SigMode,channel,buffer1,0);
//        if(RM6D010_state == 2)
//        {
//            buffer1[0] = 0x02;
//            buffer1[1] = 0x53;
//            buffer1[2] = 0x20;         //Brightness control is on
//            SSD2828_W_Array(SigMode,channel,buffer1,0);
//        }
//        else if(RM6D010_state == 1)
//        {
//            buffer1[0] = 0x02;
//            buffer1[1] = 0x38;
//            buffer1[2] = 0x00;         //Exit idle 
//            SSD2828_W_Array(SigMode,channel,buffer1,0);  
//        }  
        
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x02;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        RM6D010_state = 0;
    }
    else if(buffer[3] == 0x02)
    {
//        buffer1[0] = 0x02;
//        buffer1[1] = 0xFE;
//        buffer1[2] = 0x00;
//        SSD2828_W_Array(SigMode,channel,buffer1,0);
//        if(RM6D010_state == 2)
//        {
//            buffer1[0] = 0x02;
//            buffer1[1] = 0x53;
//            buffer1[2] = 0x20;
//            SSD2828_W_Array(SigMode,channel,buffer1,0);
//        }
//        buffer1[0] = 0x02;
//        buffer1[1] = 0x39;
//        buffer1[2] = 0x00;            //Enter idle
//        SSD2828_W_Array(SigMode,channel,buffer1,0);
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x03;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        RM6D010_state = 1;
       
    }   
    else if(buffer[3] == 0x03)
    {
//        buffer1[0] = 0x02;
//        buffer1[1] = 0xFE;
//        buffer1[2] = 0x00;
//        SSD2828_W_Array(SigMode,channel,buffer1,0);
//        if(RM6D010_state == 1)
//        {
//            buffer1[0] = 0x02;
//            buffer1[1] = 0x38;
//            buffer1[2] = 0x00;
//            SSD2828_W_Array(SigMode,channel,buffer1,0);
//        }
//        buffer1[0] = 0x02;
//        buffer1[1] = 0x53;
//        buffer1[2] = 0x60;
//        SSD2828_W_Array(SigMode,channel,buffer1,0);
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x03;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        RM6D010_state = 2;
    }
    
    delay_ms(5);
    buffer1[0] = 0x02;
    buffer1[1] = 0xEF;
    buffer1[2] = 0x01;
    SSD2828_W_Array(SigMode,channel,buffer1,0);
    delay_ms(50);    
    
		buffer1[0] = 0x02;
		buffer1[1] = 0xF0;
		buffer1[2] = 0x01;    
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		buffer1[0] = 0x02;
		buffer1[1] = 0xEF;
		buffer1[2] = 0x00;    
		SSD2828_W_Array(SigMode,channel,buffer1,0);     
		delay_ms(5);
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		buffer[4] = Uart_Error_None;
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok  
}


void ProcessForIc3D( USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	//void *pdev;
	switch(buffer[1])
	{
		case 0x01:                                      //写寄存器
			RM6D01X_Write_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
		case 0x02:                                      //读寄存器
			RM6D01X_Read_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
		case 0x08:                                      //写Gamma
			RM6D01X_Write_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
		case 0x09:                                      //读Gamma
			RM6D01X_Read_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data); break;
		case 0x0A:			    //otp GammaSet1					//OTP Start
		case 0x0D:          //otp GammaSet2    
		case 0x0E:          //otp GammaSet3
			RM6D01X_Gamma_OTP_Start(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data); break;
		case 0x0B:                                      //Read OTP Time
			RM6D01X_Read_OTP_Time(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data); break;
		case 0x11:                                      // auto Gamma
			RM6D01x_Gamma_switch(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
		case 0xDD:                                      // auto Gamma
			RM6D01X_auto_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
		default:break;
	}			
}


